
const baseUrl = 'http://ls.ngrok2.xiaomiqiu.cn'

// const baseUrl = 'http://192.168.1.14:4006'
// 请求类型 query body
const query = {'content-type':'application/x-www-form-urlencoded'} 
const body = {'content-type':'application/json'}


class HTTP{
	constructor() {}
	get(url,data = {},type = false){
		console.log('get请求',data)
		return new Promise((resolve,reject)=>{
			uni.request({
				url:baseUrl+url,
				method:'GET',
				header:type?body:query,
				data:data,
				success: (res) => {
					if(res.data.code != 0){
						uni.showToast({
							title:res.data.code+res.data.msg,
							icon:'none'
						})
					}
					
					if(res.data.code ==0){
					return resolve(res)
					}
					
				},
				fail: (error) => {
					uni.showToast({
						title:error,
						icon:'none'
					})
					return reject(error)
				}
			})
		})
	}
	
	getlogout(url,data = {},type = false){
		return new Promise((resolve,reject)=>{
			uni.request({
				url:baseUrl+url,
				method:'GET',
				header:data,
				data:data,
				success: (res) => {
					if(res.data.code != 0){
						uni.showToast({
							title:res.data.code+res.data.msg,
							icon:'none'
						})
					}
					
					if(res.data.code ==0){
					return resolve(res)
					}
					
				},
				fail: (error) => {
					uni.showToast({
						title:error,
						icon:'none'
					})
					return reject(error)
				}
			})
		})
	}
	
	gettoken(url,data = {},token,type = false){
		return new Promise((resolve,reject)=>{
			uni.request({
				url:baseUrl+url,
				method:'GET',
				header:{token:token},
				data:data,
				success: (res) => {
					if(res.data.code != 0){
						uni.showToast({
							title:res.data.code+res.data.msg,
							icon:'none'
						})
					}
					
					if(res.data.code ==0){
					return resolve(res)
					}
					
				},
				fail: (error) => {
					uni.showToast({
						title:error,
						icon:'none'
					})
					return reject(error)
				}
			})
		})
	}
	
	post(url,data = {},type = false,loading = false){
		if(loading){
			uni.showLoading({})
		}
		return new Promise((resolve,reject)=>{
		
			uni.request({
				url:baseUrl+url,
				method:'POST',
				header:type?body:query,
				data:data,
				success: (res) => {
					uni.hideLoading()
					if(type){
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						})
					}
					if(res.data.code != 0){
						uni.showToast({
							title:res.data.code+res.data.msg,
							icon:'none'
						})
					}
					if(res.data.code ==0){
						
					return resolve(res)
					}
					
				},
				fail: (error) => {
					uni.showToast({
						title:error,
						icon:'none'
					})
					return reject(error)
				}
			})
		})
	}
	
	posttoken(url,data = {},token,type = false,loading = false){
		if(loading){
			uni.showLoading({})
		}
		console.log(data)
		return new Promise((resolve,reject)=>{
		
			uni.request({
				url:baseUrl+url,
				method:'POST',
				header:{
					'content-type':'application/json',
					token
				},
				data:data,
				success: (res) => {
					uni.hideLoading()
					console.log(res)
					if(type){
						// uni.showToast({
						// 	title:res.data.msg,
						// 	icon:'none'
						// })
					}
					if(res.data.code != 0){
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						})
					}
					if(res.data.code ==0){
						
					return resolve(res)
					}
					
				},
				fail: (error) => {
					uni.showToast({
						title:error,
						icon:'none'
					})
					return reject(error)
				}
			})
		})
	}
	
	
	posttokenquery(url,data = {},token,type = false,loading = false){
		if(loading){
			uni.showLoading({})
		}
		console.log(data)
		return new Promise((resolve,reject)=>{
		
			uni.request({
				url:baseUrl+url,
				method:'POST',
				header:{
					'content-type':'application/x-www-form-urlencoded',
					token
				},
				data:data,
				success: (res) => {
					uni.hideLoading()
					console.log(res)
					if(type){
						// uni.showToast({
						// 	title:res.data.msg,
						// 	icon:'none'
						// })
					}
					if(res.data.code != 0){
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						})
					}
					if(res.data.code ==0){
						
					return resolve(res)
					}
					
				},
				fail: (error) => {
					uni.showToast({
						title:error,
						icon:'none'
					})
					return reject(error)
				}
			})
		})
	}
	
	uploadImage(url,data,token){
			uni.showLoading({
				title:'上传中'
			})
		return new Promise((resolve,reject)=>{
			uni.uploadFile({
				url: baseUrl+url,
				filePath: data,
				name: 'file',
				header:{token},
				method:'POST',
				success: (uploadFileRes) => {
					console.log(uploadFileRes)
					let da = JSON.parse(uploadFileRes.data)
					return resolve(da.data)
				},
				fail:(error)=>{
					uni.showToast({
						title:'上传失败',
						icon:'none'
					})
					return reject(error)
				}
			});
		})
	}
	
	
	uploadaudio(url,data){
			uni.showLoading({
				title:'上传中'
			})
		return new Promise((resolve,reject)=>{
			uni.uploadFile({
				url: baseUrl+url,
				filePath: data,
				name: 'file',
				success: (uploadFileRes) => {
					console.log('luyin',uploadFileRes)
					let da = JSON.parse(uploadFileRes.data)
					console.log('上传的是録音',da)
					return resolve(da)
				},
				fail:(error)=>{
					uni.showToast({
						title:'上传失败',
						icon:'none'
					})
					return reject(error)
				}
			});
		})
	}
}

 export default HTTP